package vsvm.data.filter.ranking.rankingStuff;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;

import javax.swing.JFileChooser;
import javax.swing.JFrame;

import vsvm.data.model.AbstractAttribute;

public class SaveSelected implements Serializable {

	private static final long serialVersionUID = 100L;
	private boolean file = false;
	
	private ArrayList<AbstractAttribute> selectedAttributes = null;
	private String path = ".";
	private JFileChooser fc = null;
	
	public SaveSelected()
	{
		fc = new JFileChooser(getPath());
	}
	
	public JFileChooser getFc() {
		return fc;
	}

	public void setFc(JFileChooser fc) {
		this.fc = fc;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public ArrayList<AbstractAttribute> getSelectedAttributes() {
		return selectedAttributes;
	}

	public void setSelectedAttributes(
			ArrayList<AbstractAttribute> selectedAttributes) {
		this.selectedAttributes = selectedAttributes;
	}

	public boolean isFile() {
		return file;
	}

	public void setFile(boolean file) {
		this.file = file;
	}
	
	public void saveSelected(Object[] arr)
	{
		
		ArrayList<AbstractAttribute> list = new ArrayList<AbstractAttribute>();
		
		for (int i = 0; i < arr.length; ++i)
		{
			list.add((AbstractAttribute)arr[i]);	
		}
		
		setSelectedAttributes(list);

		if(isFile() && arr.length > 0)
		{
			StringBuffer tmp = new StringBuffer(((AbstractAttribute)arr[0]).toString());
			
			for (int i = 1; i < arr.length; ++i)
			{
				tmp.append("," + ((AbstractAttribute)arr[i]).toString());
			}

			fc = new JFileChooser(getPath());
			int returnVal = fc.showSaveDialog(new JFrame());
			
			if(returnVal == JFileChooser.APPROVE_OPTION)
			{						
					File file = fc.getSelectedFile();
					setPath(file.getPath());
					
					try 
					{
						file.createNewFile();
						BufferedWriter output = new BufferedWriter(new FileWriter(file));
						output.write(tmp.toString());
						output.newLine();
						output.close();
					} 
					catch (IOException e1) 
					{
						e1.printStackTrace();
					}
			}
		}
	}

}
